package com.example.libo.huihui.adapter;

import android.content.Context;
import android.support.v4.util.Pair;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.libo.huihui.BR;
import com.example.libo.huihui.R;
import com.example.libo.huihui.been.GuoneiListEntity;
import com.example.libo.huihui.been.HaitaoEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by libo on 2017/1/12.
 */

public class HaitaoAdapter extends RecyclerView.Adapter<HaitaoAdapter.MyViewHolder>{
    private Context context;
    private ArrayList<HaitaoEntity.DataBean.ListBeanX> datas;

    public HaitaoAdapter(Context context, ArrayList datas){
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = null;
        switch (viewType){
            case 1:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_allbuy,parent,false);
                break;
            case 2:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_huizixun,parent,false);
                break;
            case 3:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_quality,parent,false);
                break;
            case 4:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_rexiao,parent,false);
                break;
            default:
                itemView = new View(context);
                break;
        }
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case 1:
                HaitaoEntity.DataBean.ListBeanX.HaitaoItemBean entity= datas.get(position).getHaitaoItem();
                TextView tvTitle = (TextView) holder.getView(R.id.tv_title);
                ImageView imageView = (ImageView) holder.getView(R.id.iv_cover);
                TextView tvFrom = (TextView) holder.getView(R.id.tv_from);
                TextView tvSold = (TextView) holder.getView(R.id.tv_selled);
                TextView tvPrice = (TextView) holder.getView(R.id.tv_price);

                tvFrom.setText(entity.getMerchant().getName());
                tvSold.setText("已售" + entity.getSalesCount() + "件");
                tvTitle.setText(entity.getTitle());
                tvPrice.setText(entity.getPrice());
                Picasso.with(context).load(entity.getImageUrl()).into(imageView);
                break;

            case 2:
                HaitaoEntity.DataBean.ListBeanX.HuiHuodongBean entity2 = datas.get(position).getHuiHuodong();
                TextView tvHTitle = (TextView) holder.getView(R.id.tv_huizixun_title);
                TextView tvHContent = (TextView) holder.getView(R.id.tv_huizixun_content);
                ImageView ivCover = (ImageView) holder.getView(R.id.iv_huizixun_cover);
                TextView tvTag = (TextView) holder.getView(R.id.tv_tag);
                ImageView ivTag = (ImageView) holder.getView(R.id.iv_tag);
                tvTag.setText(entity2.getGroupName());
                tvHTitle.setText(entity2.getTitle());
                tvHContent.setText(entity2.getSummary());
                Picasso.with(context).load(entity2.getImageUrl()).into(ivCover);
                Picasso.with(context).load(entity2.getGroupImage()).into(ivTag);
                break;

            case 3:
                HaitaoEntity.DataBean.ListBeanX.HuiItemBean entity3 = datas.get(position).getHuiItem();
                ImageView qualityCover = (ImageView) holder.getView(R.id.iv_cover);
                TextView qualitytvTag = (TextView) holder.getView(R.id.tv_tag);
                ImageView qualityivTag = (ImageView) holder.getView(R.id.iv_tag);
                TextView qualityTitle = (TextView) holder.getView(R.id.tv_title);
                TextView qualityContent = (TextView) holder.getView(R.id.tv_content);
                TextView qualityFrom = (TextView) holder.getView(R.id.tv_from);

                qualitytvTag.setText(entity3.getGroupName());
                qualityTitle.setText(entity3.getTitle());
                qualityContent.setText(entity3.getSubTitle());
                qualityFrom.setText(entity3.getMerchant().getName());
                Picasso.with(context).load(entity3.getImageUrl()).into(qualityCover);
                Picasso.with(context).load(entity3.getGroupImage()).into(qualityivTag);
                break;

            case 4:
                HaitaoEntity.DataBean.ListBeanX.TopicGroupBean entity4 = datas.get(position).getTopicGroup();
                ImageView rexiaoivTag = (ImageView) holder.getView(R.id.iv_tag);
                TextView rexiaotvTag = (TextView) holder.getView(R.id.tv_tag);
                TextView rexiaoTitle = (TextView) holder.getView(R.id.tv_title);

                rexiaoTitle.setText(entity4.getTitle());
                rexiaotvTag.setText(entity4.getGroupName());
                Picasso.with(context).load(entity4.getGroupImage()).into(rexiaoivTag);

                List list = entity4.getList();
                HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap();
                map.put(HaitaoEntity.DataBean.ListBeanX.TopicGroupBean.ListBean.class,Pair.create(R.layout.item_rexiao2,BR.rexiao));
                BindAdapter bindAdapter = new BindAdapter(map,list,context);
                RecyclerView rv = (RecyclerView) holder.getView(R.id.rv_rexiao);
                rv.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
                rv.setAdapter(bindAdapter);
                break;
//
//            case 3:
//                List list = datas.get(position).getHotSearch().getList();
//                HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap();
//                map.put(GuoneiListEntity.DataBean.ListBeanX.HotSearchBean.ListBean.class,Pair.create(R.layout.item_hot,BR.hotsearch));
//                BindAdapter bindAdapter = new BindAdapter(map,list,context);
//                RecyclerView recyclerView = (RecyclerView) holder.getView(R.id.rv_hotsearch);
//                recyclerView.setLayoutManager(new GridLayoutManager(context,4));
//                recyclerView.setAdapter(bindAdapter);
//                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        String type = datas.get(position).getType();
        if(type.equals("HaitaoItem")){
            return 1;
        }else if(type.equals("HuiHuodong")){
            return 2;
        }else if(type.equals("HuiItem")){
            return 3;
        }else if(type.equals("TopicGroup")){
            return 4;
        }
// else if(type.equals("HuiZixun")){
//            return 2;
//        }else if(type.equals("HotSearch")){
//            return 3;
//        }

        return 0;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private Map<Integer,View> map;

        public MyViewHolder(View itemView) {
            super(itemView);
            map = new HashMap<>();
        }

        public View getView(int resId){
            View view = null;
            if(map.containsKey(resId)){  //如果map存过这个key-id，则直接获取，否则就要findViewbyId
                map.get(resId);
            }else{
                view = itemView.findViewById(resId);
            }
            return view;
        }
    }

}
